package com.ruoyi.project.system.fund.controller;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.debt.domain.Stage;
import com.ruoyi.project.system.debt.domain.StageView;
import com.ruoyi.project.system.fund.domain.Pay;
import com.ruoyi.project.system.fund.domain.PayView;
import com.ruoyi.project.system.holder.domain.Holder;
import com.ruoyi.project.system.holder.service.IHolderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.fund.domain.Fund;
import com.ruoyi.project.system.fund.service.IFundService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 资金管理Controller
 * 
 * @author xn
 * @date 2024-10-06
 */
@Controller
@RequestMapping("/system/fund")
public class FundController extends BaseController
{
    private String prefix = "system/fund";

    @Autowired
    private IFundService fundService;

    @Autowired
    private IHolderService holderService;

    @RequiresPermissions("system:fund:view")
    @GetMapping()
    public String fund()
    {
        return prefix + "/fund";
    }

    /**
     * 查询资金管理列表
     */
    @RequiresPermissions("system:fund:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Fund fund)
    {
        startPage();
        List<Fund> list = fundService.selectFundList(fund);
        return getDataTable(list);
    }

    /**
     * 导出资金管理列表
     */
    @RequiresPermissions("system:fund:export")
    @Log(title = "资金管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Fund fund)
    {
        List<Fund> list = fundService.selectFundList(fund);
        ExcelUtil<Fund> util = new ExcelUtil<Fund>(Fund.class);
        return util.exportExcel(list, "资金管理数据");
    }

    /**
     * 新增资金管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("holders", holderService.selectHolderAll());
        return prefix + "/add";
    }

    /**
     * 新增保存资金管理
     */
    @RequiresPermissions("system:fund:add")
    @Log(title = "资金管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Fund fund)
    {
        return toAjax(fundService.insertFund(fund));
    }

    /**
     * 修改资金管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Fund fund = fundService.selectFundById(id);
        mmap.put("fund", fund);
        return prefix + "/edit";
    }

    /**
     * 支付管理
     */
    @GetMapping("/pay/{id}")
    public String pay(@PathVariable("id") Long id, ModelMap mmap)
    {
        Fund fund = fundService.selectFundById(id);
        List<PayView> pay = fundService.selectPayById(id);

        mmap.put("fund", fund);
        mmap.put("pays", pay);
        return prefix + "/pay";
    }

    /**
     * 修改保存资金管理
     */
    @RequiresPermissions("system:fund:edit")
    @Log(title = "资金管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Fund fund)
    {
        return toAjax(fundService.updateFund(fund));
    }

    /**
     * 删除资金管理
     */
    @RequiresPermissions("system:fund:remove")
    @Log(title = "资金管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fundService.deleteFundByIds(ids));
    }

    /**
     * 新增保存支付管理
     */
    @RequiresPermissions("system:debt:add")
    @Log(title = "支付管理", businessType = BusinessType.INSERT)
    @PostMapping("/addPay")
    @ResponseBody
    public AjaxResult addPay(String data)
    {

        JSONArray jsonArray = JSON.parseArray(data);
        JSONObject jsObj = (JSONObject) JSONObject.parse(String.valueOf(jsonArray.get(0)));
        if(jsObj !=  null){
            fundService.delPay(Long.parseLong(jsObj.get("value").toString()));
        }
        int a = (jsonArray.size()-1)/4;

        String pattern = "\\[(.*?)\\]";
        Pattern r = Pattern.compile(pattern);
        for (int i = 0; i < a; i++) {
            Pay pay = new Pay();
            for (int j = 0; j < jsonArray.size(); j++) {
                JSONObject jsonObject = (JSONObject) JSONObject.parse(String.valueOf(jsonArray.get(j)));
                String aa = String.valueOf(jsonObject.get("name"));
                if("id".equals(aa)){
                    pay.setFundId(Integer.parseInt(jsonObject.get("value").toString()));
                }
                Matcher m = r.matcher(aa);
                if (m.find()) {
                    if(m.group(1).equals(String.valueOf(i))){
                        if(aa.contains("amt")){
                            pay.setAmt(String.valueOf(jsonObject.get("value")));
                        } else if (aa.contains("payDate")) {
                            pay.setPayDate(String.valueOf(jsonObject.get("value")));
                        } else if (aa.contains("his")) {
                            pay.setHis(String.valueOf(jsonObject.get("value")));
                        }
                    }
                }
            }
            pay.setCreateBy(ShiroUtils.getLoginName());
            pay.setCreateTime(new Date());
            fundService.insertPay(pay);
        }
        return toAjax(1);
    }
}
